package LeetCode;

import java.awt.print.Printable;
import java.lang.reflect.Array;
import java.util.ArrayList;

import LinkedListAndArray.List;

public class _203_移除链表元素 {
	public static ListNode removeElements(ListNode head, int val) {
		if (head == null) return head;
		head.next = removeElements(head.next, val);
		return head.val == val ? head.next : head;
	}

	public static ListNode removeElements1(ListNode head, int val) {
		ListNode newListNode = new ListNode(0, null);
		ListNode currentListNode = newListNode;
		while (head != null) {
			if (head.val != val) {
				currentListNode.next = new ListNode(head.val, null);
				currentListNode = currentListNode.next;
			}
			head = head.next;
		}
		return newListNode.next;
	}
	
	public static void main(String[] args) {
		int list[] = {1,2,2,6,3,3,4,5,6};
		ListNode newListNode = new ListNode(0, null);
		ListNode currentListNode = newListNode;
		for (int i = 0; i < list.length; i++) {
			currentListNode.next = new ListNode((int)list[i], null);
			currentListNode = currentListNode.next;
		}
		
		currentListNode = newListNode.next;
		for (int i = 0; i < list.length; i++) {
			System.out.println("" + currentListNode.val);
			currentListNode = currentListNode.next;
		}
		
		currentListNode = removeElements1(newListNode.next, 6);
		System.out.println("===================");
		for (int i = 0; i < 7; i++) {
			System.out.println("" + currentListNode.val);
			currentListNode = currentListNode.next;
		}
		
		System.out.println("-------------------");
		currentListNode = removeElements(newListNode.next, 6);
		for (int i = 0; i < 7; i++) {
			System.out.println("" + currentListNode.val);
			currentListNode = currentListNode.next;
		}
	}
}
